#import "FifteenPuzzleViewController.h"
#import "FifteenPuzzleAppDelegate.h"
#import "FifteenPuzzleModel.h"
#import "Tile.h"
#import "TileView.h"
#import "Position.h"

@implementation FifteenPuzzleViewController
@synthesize moveCountLabel;
@synthesize timeWatchLabel;

-(id) init {
    [super init];
    model = [[FifteenPuzzleModel alloc] init];
    solvedState = [[NSMutableArray alloc] initWithCapacity:15];
    for (int i=0; i<15; i++) {
        Tile * tile = [[Tile alloc] init];
        tile.position = [[Position alloc] initWithCol:i%4 andRow:i/4];
        [solvedState addObject:tile];        
    }
    
    canPlay = FALSE;
    firstMove = FALSE;
    return model;
}

- (void)showActivity
{
    nowInterval = [NSDate timeIntervalSinceReferenceDate];
    activityInterval = nowInterval - startInterval;
    //NSLog(@"%f", activityInterval);
    timeWatchLabel.text = [NSString stringWithFormat:@"%3$f", activityInterval];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tileViewCollection = [[NSMutableArray alloc] init];
    for(Tile * tile in model.tileCollection) {
        TileView * tileView = [[TileView alloc] initWithFrame:tile.rect andLabel: tile.n];
        [tileViewCollection addObject:tileView];
        [self.view addSubview:tileView];
        [self.view retain];
    }
}

- (void) moveTileOn: (int)position withDirection: (int)direction {
    for(TileView * tile in tileViewCollection) {
        if (tile.tag == position) {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration: kAnimationDuration];
            switch (direction) {
                case 0:
                    tile.center = CGPointMake(tile.center.x + 70, tile.center.y);
                    break;
                case 1:
                    tile.center = CGPointMake(tile.center.x, tile.center.y + 70);
                    break;
                case 2:
                    tile.center = CGPointMake(tile.center.x - 70, tile.center.y);
                    break;
                case 3:
                    tile.center = CGPointMake(tile.center.x, tile.center.y - 70);
                    break;
                default:
                    break;
            }
            [model update: tile.tag rect: tile.frame];
            [UIView commitAnimations];
            if (firstMove) {
                startInterval = [NSDate timeIntervalSinceReferenceDate];
                timer =[NSTimer scheduledTimerWithTimeInterval:0.01 target:self
                                                      selector:@selector(showActivity) userInfo:nil repeats:YES];
                firstMove = FALSE;
            }
                
            moveCount++;
            canPlay = ![self check];
            if (!canPlay) {
                [timer invalidate];
            }
            moveCountLabel.text = [NSString stringWithFormat:@"%d", moveCount];
        }
    }
}

-(BOOL) check {
    BOOL result = TRUE;
    for (int i = 0; i<15; i++) {
        Tile *sTile = [solvedState objectAtIndex:i];
        Tile *cTile = [model.tileCollection objectAtIndex:i];
        if (sTile.position.col != cTile.position.col
            || sTile.position.row != cTile.position.row) {
            result = FALSE;
            break;
        }
    }
    return result;
}

-(IBAction) shuffle {
    moveCount = 0;
    moveCountLabel.text = [NSString stringWithFormat:@"%d", moveCount];
    canPlay = TRUE;
    firstMove = TRUE;
    
}

-(void) moveTileWithIndex: (int)index {
    if (canPlay)
        [model moveTileWithIndex:index];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [moveCountLabel release];
    [model release];
    [tileViewCollection release];
    [super dealloc];
}
@end
